package com.lizheng.yunservice.culturalld.service.impl;

import com.lizheng.yunservice.common.exception.ServiceException;
import com.lizheng.yunservice.culturalld.model.VO.VtpCulturalDetaiwenhuaxiangqilVO;
import com.lizheng.yunservice.culturalld.service.VtpCulturalwenhuaxiangqiService;
import com.lizheng.yunservice.travel.doamin.VtpCultural;
import com.lizheng.yunservice.travel.mapper.VtpCulturalMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class VtpCulturalServicewenhuaxiangqiImpl implements VtpCulturalwenhuaxiangqiService {

    private final VtpCulturalMapper culturalMapper;

    public VtpCulturalServicewenhuaxiangqiImpl(VtpCulturalMapper culturalMapper) {
        this.culturalMapper = culturalMapper;
    }

    @Override
    public VtpCulturalDetaiwenhuaxiangqilVO getCulturalDetail(Long culturalId) {
        VtpCultural entity = culturalMapper.selectById(culturalId);
        if (entity == null || entity.getDeleteFlag() == 1) {
            throw new ServiceException("文创不存在或已删除");
        }

        VtpCulturalDetaiwenhuaxiangqilVO vo = new VtpCulturalDetaiwenhuaxiangqilVO();
        BeanUtils.copyProperties(entity, vo);

        // Java 8兼容的媒体资源处理
        vo.setImgs(parseMediaUrls(entity.getLogoUrl()));
        vo.setVideos(handleVideoUrl(entity.getVideoUrl()));

        return vo;
    }

    // Java 8兼容的URL解析方法
    private List<String> parseMediaUrls(String urls) {
        if (urls == null || urls.isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.asList(urls.split("\\s*,\\s*"));
    }

    // Java 8兼容的视频URL处理
    private List<String> handleVideoUrl(String videoUrl) {
        return videoUrl == null ?
                Collections.emptyList() :
                Collections.singletonList(videoUrl);
    }
}